<?php

namespace Addons\OaSystem\Controller;
use Think\ManageBaseController;

class OaBaseController extends ManageBaseController{
    protected $appId;
    protected $cur_user_info;
    protected $cur_emp_id;
    protected $user_roleids;
    protected $first_sub_nav_url;
    protected $dutylist;
    protected $leaderDutys;
    protected $workTime;

    function _initialize() {

        //$this->model = $this->getModel ( 'oa_user' );
        parent::_initialize ();

        $this->appId = get_app_info()['appid'];
        $this->cur_user_info = $this->getCurUserInfo();
        $this->cur_emp_id = empty($this->cur_user_info) ? null : $this->cur_user_info['emp_id'];
        $user_roles = db_select('oa_user_role', ['emp_id'=>$this->cur_emp_id]);
        $this->user_roleids = array_column($user_roles, 'role_id');
        if($this->mid == 1) {
            $roles = db_select('oa_role');
            $this->user_roleids = array_column($roles, 'id');
        }
        $this->dutylist = $this->getDutyList();
        $this->leaderDutys = $this->getLeaderDutys();
        $this->workTime = $this->getWorkTimeSetting();

        $map['appid'] = $this->appId;
        session('common_condition', $map);

        D('OaSysLog')->addLog($this->cur_emp_id, CONTROLLER_NAME, ACTION_NAME);

        /* 管理中心的导航 */
        $menus = $this->get_menus();
        $sub_nav = $this->get_sub_nav($menus);
        $nav = $this->get_nav($menus);
        $this->first_sub_nav_url = $sub_nav[0]['url'];

        $this->assign ( $menus );
        $this->_nav ();
        $this->assign('nav', $nav);
        $this->assign('sub_nav', $sub_nav);
    }

    private function get_menus() {
        $menus = D ( 'Common/Menu' )->get ();
        if($this->mid == 1)
            return $menus;

        $map['pid'] = 0;
        foreach ($menus['core_side_menu'] as $i => $v) {
            $map['title'] = $v['title'];
            $nav_menu = db_find('oa_nav_menu', $map);
            $menu_roles = json_decode($nav_menu['roles'], true);
            if(array_intersect($this->user_roleids, $menu_roles) == null) {
                unset($menus['core_side_menu'][$i]);
            }
        }

        $menus['core_side_menu'] = array_values($menus['core_side_menu']);
        $find_active = false;
        foreach ($menus['core_side_menu'] as $v) {
            if($v['class'] == 'active current')
                $find_active = true;
        }
        if(!$find_active)
            $menus['core_side_menu'][0]['class'] = 'active current';
        return $menus;
    }

    private function get_nav($menus) {
        foreach ($menus['core_side_menu'] as $i => $v) {
            if ($v['class'] == 'active current')
                break;
        }
        $curSideMenu = $menus['core_side_menu'][$i];

        $nav_menu = db_find('oa_nav_menu', ['pid'=>0,'title'=>$curSideMenu['title']]);
        $res ['title'] = $curSideMenu['title'];
        $res ['url'] = $curSideMenu['url'];
        $res ['class'] = $this->getClass($nav_menu['controller'], $nav_menu['action']);
        $nav [] = $res;
        return $nav;
    }

    private function get_sub_nav(&$menus) {
        foreach ($menus['core_side_menu'] as $i => $v) {
            if ($v['class'] == 'active current')
                break;
        }
        $curSideMenu = $menus['core_side_menu'][$i];

        $db_sub_navs = db_select('oa_nav_menu', ['pid'=>$curSideMenu['id']], 'sort asc');
        foreach ($db_sub_navs as $v) {
            $sub_nav_roles = json_decode($v['roles'], true);
            if(array_intersect($this->user_roleids, $sub_nav_roles) != null) {
                $res ['title'] = $v['title'];
                $res ['url'] = addons_url('OaSystem://'.$v['controller'].'/'.$v['action'], array('mdm' => I('mdm')));
                $res ['class'] = $this->getClass($v['controller'], $v['action']);
                $sub_nav[] = $res;
            }
        }
        if(!empty($sub_nav)) {
            $menus['core_side_menu'][$i]['url'] = $sub_nav[0]['url'];
        }

        return $sub_nav;
    }

    private function getClass($menu_controller, $menu_action) {
        $controller = strtolower(CONTROLLER_NAME);
        $action = strtolower(ACTION_NAME);
        $menu_controller = strtolower($menu_controller);
        $menu_action = strtolower($menu_action);
        $class = ($controller==$menu_controller && $action==$menu_action) ? 'cur' : '';
        return $class;
    }

    function getCurUserInfo() {
        if(($employee = db_find('oa_employee', ['userid'=>$this->mid])) == NULL) {
            return false;
        }

        $dept = db_find('oa_dept', ['id'=>$employee['dept_id']]);
        $adminLvl = db_find('oa_emp_admin_level', ['id'=>$employee['adminLvl_id']]);
        $duty = db_find('oa_duty', ['id'=>$employee['duty_id']]);
        if(empty($dept) || empty($adminLvl) || empty($duty)) {
            return false;
        }

        $userInfo = array(
            'name' => $employee['name'],
            'emp_id' => $employee['id'],
            'duty' => end(explode('_', $duty['name'])),
            'duty_id' => $duty['id'],
            'dept' => $dept['name'],
            'dept_id' => $dept['id'],
            'dept_no' => $dept['dept_no'],
            'adminLvl' => $adminLvl['name'],
            'adminLvl_id' => $adminLvl['id'],
            'admin_level' => $adminLvl['level']
        );
        return $userInfo;
    }

    function getUserInfo($emp_id) {
        if(($employee = db_find('oa_employee', ['id'=>$emp_id])) == NULL) {
            return false;
        }

        $dept = db_find('oa_dept', ['id'=>$employee['dept_id']]);
        $adminLvl = db_find('oa_emp_admin_level', ['id'=>$employee['adminLvl_id']]);
        $duty = db_find('oa_duty', ['id'=>$employee['duty_id']]);
        if(empty($dept) || empty($adminLvl) || empty($duty)) {
            return false;
        }

        $userInfo = array(
            'name' => $employee['name'],
            'emp_id' => $employee['id'],
            'duty' => end(explode('_', $duty['name'])),
            'duty_id' => $duty['id'],
            'dept' => $dept['name'],
            'dept_id' => $dept['id'],
            'dept_no' => $dept['dept_no'],
            'adminLvl' => $adminLvl['name'],
            'adminLvl_id' => $adminLvl['id'],
            'admin_level' => $adminLvl['level']
        );
        return $userInfo;
    }

    function getDepts() {
        $depts = db_select('oa_dept', null, 'dept_no asc');
        foreach ($depts as $i => $v) {
            if($v['dept_grade_id'] == 3) {
                $pdept = db_find('oa_dept', ['id'=>$v['pid']]);
                $depts[$i]['name'] = $pdept['name'].'_'.$v['name'];
            }
        }
        array_unshift($depts, array('id'=>0, 'name'=>'全部'));
        return $depts;
    }

    function getRoles() {
        $roles = db_select('oa_role', null, null, 'name');
        foreach ($roles as $i => $v) {
            if(strstr($v['name'], '_')) {
                $tmp = explode('_', $v['name']);
                $roles[$i]['name'] = end($tmp);
            }
        }
        $roles1 = array_column($roles, 'name');
        $roles2 = array_unique($roles1);
        $roles = array();
        foreach ($roles2 as $i => $v) {
            $roles[$i]['id'] = $i;
            $roles[$i]['name'] = $v;
        }
        return $roles;
    }

    function getDutyList() {
        $dutys = db_select('oa_duty', null, 'duty_no asc', 'name');
        foreach ($dutys as $i => $v) {
            if(strstr($v['name'], '_')) {
                $tmp = explode('_', $v['name']);
                $dutys[$i]['name'] = end($tmp);
            }
        }
        $dutys = array_column($dutys, 'name');
        $dutys = array_unique($dutys);
        return $dutys;
    }

    function getLeaderDutys() {
        $dutys = array();
        $map['level'] = array('egt', 19);
        $data = db_select('oa_duty', $map, null, 'name');
        $dutys['guan'] = array_column($data, 'name');

        $dutys['chu'] = array();
        $map['level'] = array('in', array(15,16));
        $data = db_select('oa_duty', $map, null, 'name');
        foreach ($data as $v) {
            if(strstr($v['name'], '_')) {
                $tmp = explode('_', $v['name']);
                array_push($dutys['chu'], end($tmp));
            }
        }
        $dutys['chu'] = array_unique($dutys['chu']);

        $dutys['ke'] = array();
        $map['level'] = array('in', array(11,12));
        $data = db_select('oa_duty', $map, null, 'name');
        foreach ($data as $v) {
            if(strstr($v['name'], '_')) {
                $tmp = explode('_', $v['name']);
                array_push($dutys['ke'], end($tmp));
            }
        }
        $dutys['ke'] = array_unique($dutys['ke']);

        return $dutys;
    }

    private function getWorkTimeSetting() {
        $work_time_setting = db_find('oa_work_time_setting', ['year'=>date('Y')]);
        $clock_on = json_decode($work_time_setting['clockon'], true);
        $holidays = json_decode($work_time_setting['legal_holidays'], true);
        $adjust_rest_days = json_decode($work_time_setting['adjust_rest_days'], true);
        $adjust_work_days = json_decode($work_time_setting['adjust_work_days'], true);

        $workTime['clockon'] = $clock_on;
        $workTime['holidays'] = $holidays;
        $workTime['restdays'] = $adjust_rest_days;
        $workTime['workdays'] = $adjust_work_days;

        return $workTime;
    }



}
